import { defineStore } from "pinia";
import type { reqCategoryType } from "@/api/attr"
import {
    requestCategory1,
    requestCategory2,
    requestCategory3,
} from "@/api/attr";
import { ElMessage } from "element-plus";

//定义state类型
interface storeType {
    categoryList1: reqCategoryType,
    categoryList2: reqCategoryType,
    categoryList3: reqCategoryType,
    category1Id: null | number,
    category2Id: null | number,
    category3Id: null | number,
}
//创建attr的小仓库
const useAttrStore = defineStore("attr", {
    state: (): storeType => {
        return {
            categoryList1: [],
            categoryList2: [],
            categoryList3: [],
            category1Id: null,
            category2Id: null,
            category3Id: null,
        }
    },
    getters: {},
    actions: {
        //一级列表的请求
        async getCategory1() {
            try {
                //发送请求
                const result = await requestCategory1()
                //把一级列表的数据保存起来
                this.categoryList1 = result
            } catch (e) {
                ElMessage.error("请求一级列表失败")
            }
        },
        //二级列表的请求
        async getCategory2() {
            try {
                //发送请求
                const result = await requestCategory2((this.category1Id as number))
                //把一级列表的数据保存起来
                this.categoryList2 = result
            } catch (e) {
                ElMessage.error("请求二级列表失败")
            }
        },
        //三级列表的请求
        async getCategory3() {
            try {
                //发送请求
                const result = await requestCategory3((this.category2Id as number))
                //把一级列表的数据保存起来
                this.categoryList3 = result
            } catch (e) {
                ElMessage.error("请求三级列表失败")
            }
        },
    },
})

export default useAttrStore